package sessionBeans;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.ejb.Stateless;
import javax.persistence.NamedQuery;
import javax.persistence.Query;

import entityBeans.ContactGroup;
import entityBeans.Contact;

import sessionBeans.Interfaces.IGestContactGroupRemote;

/**
 * Session Bean implementation class GestContactGroup
 */
@Stateless(mappedName = "GestContactGroupEntity")
public class GestContactGroup extends GestCrud<ContactGroup> implements IGestContactGroupRemote {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
     * Default constructor. 
     */
    public GestContactGroup() {
        // TODO Auto-generated constructor stub
    	super(ContactGroup.class);
    }
	
	@Override
	public void delete(ContactGroup entity) {
		ContactGroup newCG = em.merge(entity);
		Set<Contact> sc = new HashSet<Contact>(newCG.getListOfContacts());
		for (Contact c : sc) {
			Contact newC = em.merge(c);
			newC.getBooks().remove(newCG);
			em.merge(newC);
		}
		em.remove(newCG);
	}
	
	public Set<ContactGroup> findAll() {
		// TODO Auto-generated method stub
		Query q = em.createQuery("FROM ContactGroup");
		return new HashSet<ContactGroup>(q.getResultList());
	}

}
